package main

import (
	"fmt"
	"io/ioutil"
	"strings"

	"github.com/spf13/cast"
)

// 生成pb时，执行该文件
func main() {
	dir, err := ioutil.ReadDir("../")
	if err != nil {
		fmt.Println("文件夹为空")
		return
	}
	for _, file := range dir {
		if file.IsDir() {
			continue
		}
		fileName := file.Name()
		filepath := "../" + fileName
		fileData, err := ioutil.ReadFile(filepath)
		if err != nil {
			fmt.Printf("ReadFile err:%s \n", err.Error())
			return
		}
		data := string(fileData)
		needBreak := false
		for _, s := range getSkipFile() {
			if strings.EqualFold(s, fileName) {
				needBreak = true
				break
			}
		}
		if needBreak {
			continue
		}
		fmt.Println("需要替换的文件如下:" + fileName)
		detailItemMap := getItemMap(fileName)
		for key, val := range detailItemMap {
			valArr := strings.Split(val, "||")
			num := 1
			if len(valArr) == 2 {
				num = cast.ToInt(valArr[1])
			}
			data = strings.Replace(data, key, valArr[0], num)
		}
		data = strings.ReplaceAll(data, ",omitempty", "")
		data = strings.ReplaceAll(data, "`json:\"-\"`", "`json:\"-\" gorm:\"-\"`")
		data = strings.ReplaceAll(data, "`json:\"-\"`", "`json:\"-\" gorm:\"-\"`")
		data = strings.ReplaceAll(data, "`json:\"-\"`", "`json:\"-\" gorm:\"-\"`")
		fileData = []byte(data)
		err = ioutil.WriteFile(filepath, fileData, 0644)
		if err != nil {
			fmt.Printf("WriteFile err:%s \n ", err.Error())
			return
		}
	}
}
func getItemMap(fileName string) map[string]string {
	//detailItemMap:=make(map[string]string)
	valMap := ResponseStatusMsg
	if val, ok := valMap[fileName]; ok {
		return val
	} else {
		return nil
	}
}

/*
	var ResponseStatusMsg = map[string]map[string]string{
		"sportNew.pb.go": {"json:\"f_home_score,omitempty\"": "json:\"f_home_score\" gorm:\"-\"",
			"json:\"f_away_score,omitempty\"": "json:\"f_away_score\" gorm:\"-\"",
			"json:\"b_home_score,omitempty\"": "json:\"b_home_score\" gorm:\"-\"",
			"json:\"b_away_score,omitempty\"": "json:\"b_away_score\" gorm:\"-\""},

		"expert.pb.go": {"json:\"user_id,omitempty\"": "json:\"-\"||4",
			"json:\"fee_type,omitempty\"": "json:\"-\"",
			"json:\"playItem,omitempty\"": "json:\"playItem\" gorm:\"-\"",
		},
		"payment.pb.go": {"json:\"user_id,omitempty\"": "json:\"-\"||2",
			"json:\"apple_client_id,omitempty\"": "json:\"-\"",
			"json:\"password,omitempty\"":        "json:\"-\"",
			"json:\"url,omitempty\"":             "json:\"-\"",
			"json:\"url_type,omitempty\"":        "json:\"-\"",
			"json:\"product_id,omitempty\"":      "json:\"-\"",
		},
	}
*/
func getSkipFile() []string {
	skipFileList := make([]string, 0)
	skipFileList = append(skipFileList, "expert.pb.go")
	skipFileList = append(skipFileList, "expert_grpc.pb.go")

	//skipFileList = append(skipFileList, "sportNew.pb.go")
	//skipFileList = append(skipFileList, "sportNew_grpc.pb.go")

	skipFileList = append(skipFileList, "payment.pb.go")
	skipFileList = append(skipFileList, "payment_grpc.pb.go")

	skipFileList = append(skipFileList, "hongbao.pb.go")
	skipFileList = append(skipFileList, "hongbao_grpc.pb.go")
	return skipFileList
}

var ResponseStatusMsg = map[string]map[string]string{
	"sportNew.pb.go": {"json:\"f_home_score,omitempty\"": "json:\"f_home_score\" gorm:\"-\"||2",
		"json:\"f_away_score,omitempty\"":       "json:\"f_away_score\" gorm:\"-\"||2",
		"json:\"b_home_score,omitempty\"":       "json:\"b_home_score\" gorm:\"-\"||2",
		"json:\"b_away_score,omitempty\"":       "json:\"b_away_score\" gorm:\"-\"||2",
		"json:\"reservation_anchor,omitempty\"": "json:\"reservation_anchor\" gorm:\"-\"||2",
	},

	"expert.pb.go": {
		"json:\"playItem,omitempty\"": "json:\"playItem\" gorm:\"-\"",
	},
	"comm.pb.go": {
		"json:\"tag_title,omitempty\"":      "json:\"tag_title\" gorm:\"-\"",
		"json:\"tag_color,omitempty\"":      "json:\"tag_color\" gorm:\"-\"",
		"json:\"tag_point,omitempty\"":      "json:\"tag_point\" gorm:\"-\"",
		"json:\"tag_type,omitempty\"":       "json:\"tag_type\" gorm:\"-\"",
		"json:\"anchor_avatars,omitempty\"": "json:\"anchor_avatars\" gorm:\"-\"",
		"json:\"anchor_ids,omitempty\"":     "json:\"anchor_ids\" gorm:\"-\"",
		"json:\"favorite,omitempty\"":       "json:\"favorite\" gorm:\"-\"",
	},
	"live.pb.go": {
		"json:\"anchor\"":             "json:\"anchor\" gorm:\"-\"||2",
		"json:\"anchors\"":            "json:\"anchors\" gorm:\"-\"||2",
		"json:\"basket_home_scores\"": "json:\"basket_home_scores\" gorm:\"-\"||2",
		"json:\"basket_away_scores\"": "json:\"basket_away_scores\" gorm:\"-\"||2",
	},
	/*"payment.pb.go": {"json:\"user_id,omitempty\"": "json:\"-\"||2",
		"json:\"apple_client_id,omitempty\"": "json:\"-\"",
		"json:\"password,omitempty\"":        "json:\"-\"",
		"json:\"url,omitempty\"":             "json:\"-\"",
		"json:\"url_type,omitempty\"":        "json:\"-\"",
		"json:\"product_id,omitempty\"":      "json:\"-\"",
	},*/
}
